package g;

import c.b.a.a.a;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.charset.Charset;
import java.util.Objects;

/* compiled from: RealBufferedSink */
public final class t implements f {
    public final e a = new e();
    public final y b;
    public boolean c;

    public t(y yVar) {
        Objects.requireNonNull(yVar, "sink == null");
        this.b = yVar;
    }

    public e A() {
        return this.a;
    }

    public a0 B() {
        return this.b.B();
    }

    public f C(byte[] bArr, int i, int i2) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.A0(bArr, i, i2);
        Z();
        return this;
    }

    public void E(e eVar, long j) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.E(eVar, j);
        Z();
    }

    public long G(z zVar) throws IOException {
        long j = 0;
        while (true) {
            long c = zVar.c(this.a, 8192);
            if (c == -1) {
                return j;
            }
            j += c;
            Z();
        }
    }

    public f H(long j) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.H(j);
        return Z();
    }

    public f I(int i) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.F0(i);
        Z();
        return this;
    }

    public f K(int i) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.E0(i);
        return Z();
    }

    public f P(int i) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        e eVar = this.a;
        Objects.requireNonNull(eVar);
        eVar.E0(b0.c(i));
        Z();
        return this;
    }

    public f R(int i) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.B0(i);
        Z();
        return this;
    }

    public f V(byte[] bArr) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.z0(bArr);
        Z();
        return this;
    }

    public f W(h hVar) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.y0(hVar);
        Z();
        return this;
    }

    public f Z() throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        e eVar = this.a;
        long j = eVar.b;
        if (j == 0) {
            j = 0;
        } else {
            v vVar = eVar.a.g;
            int i = vVar.c;
            if (i < 8192 && vVar.e) {
                j -= (long) (i - vVar.b);
            }
        }
        if (j > 0) {
            this.b.E(eVar, j);
        }
        return this;
    }

    public void close() throws IOException {
        if (!this.c) {
            Throwable th = null;
            try {
                e eVar = this.a;
                long j = eVar.b;
                if (j > 0) {
                    this.b.E(eVar, j);
                }
            } catch (Throwable th2) {
                th = th2;
            }
            try {
                this.b.close();
            } catch (Throwable th3) {
                if (th == null) {
                    th = th3;
                }
            }
            this.c = true;
            if (th != null) {
                Charset charset = b0.a;
                throw th;
            }
        }
    }

    public void flush() throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        e eVar = this.a;
        long j = eVar.b;
        if (j > 0) {
            this.b.E(eVar, j);
        }
        this.b.flush();
    }

    public boolean isOpen() {
        return this.c ^ 1;
    }

    public f j0(String str) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.G0(str);
        Z();
        return this;
    }

    public f k0(long j) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        this.a.k0(j);
        Z();
        return this;
    }

    public String toString() {
        StringBuilder g = a.g("buffer(");
        g.append(this.b);
        g.append(")");
        return g.toString();
    }

    public int write(ByteBuffer byteBuffer) throws IOException {
        if (this.c) {
            throw new IllegalStateException("closed");
        }
        int write = this.a.write(byteBuffer);
        Z();
        return write;
    }
}
